package com.heaven.service.impl;

import com.heaven.core.dao.UserDao;
import com.heaven.core.entity.UserEntity;
import com.heaven.core.framework.PzServiceImpl;
import com.heaven.customerServer.customerController.vo.login.LoginRes;
import com.heaven.customerServer.customerController.vo.user.UserInfoRes;
import com.heaven.exception.BizCodeEnum;
import com.heaven.exception.RRException;
import com.heaven.service.UserService;
import com.heaven.service.minio.MinIoService;
import com.heaven.utils.BeanUtils;
import jakarta.annotation.Resource;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service("userService")
public class UserServiceImpl extends PzServiceImpl<UserDao, UserEntity> implements UserService {
    @Resource
    private MinIoService minIoService;

    @Override
    public UserEntity initUser(UserEntity userEntity) {
        if(userEntity == null) {
            return null;
        }

        if(userEntity.getHeader() != null && userEntity.getHeader().contains(":")) {
            String[] split = userEntity.getHeader().split(":");
            String headerUrl = minIoService.getObjectUrl(split[0], split[1], 1, TimeUnit.DAYS);
            userEntity.setHeader(headerUrl);
        }

        return userEntity;
    }

    @Override
    public UserInfoRes getUserInfo(String userId, LoginRes.UserSession session) {
        UserEntity userEntity = selectById(userId);
        if(userEntity == null) {
            throw new RRException(BizCodeEnum.USER_NOT_EXIST, userId);
        }
        userEntity = initUser(userEntity);
        UserInfoRes info = new UserInfoRes();
        BeanUtils.copyBeanProp(info, userEntity);
        info.setSubscribe(false);

        if(session != null) {
            UserEntity sessionUser = selectById(session.getUserId());
            info.setSubscribe(sessionUser.getSubscribeUsers().contains(userId));

        }
        return info;
    }

    /**
     * session用户订阅操作
     *
     * @param id  订阅对象id
     * @param session 登录 session
     * @param type 1：订阅用户；2：订阅视频；3：订阅文贴；4：订阅聊天对象
     */
    @Override
    public void subscribe(String id, LoginRes.UserSession session, int type) {
        if(type < 1 || type > 4) {
            throw new RRException(BizCodeEnum.SUBSCRIBE_TYPE_EXCEPTION);
        }
        UserEntity sessionUser = selectById(session.getUserId());
        if(sessionUser == null) {
            throw new RRException(BizCodeEnum.USER_NOT_EXIST, session.getUserId());
        }

        Query query = Query.query(Criteria.where("_id").is(session.getUserId()));
        switch (type) {
            case 1:
                setElement(sessionUser.getSubscribeUsers(), id);
                updateOne(query, "subscribe_users", sessionUser.getSubscribeUsers());
                break;
            case 2:
                setElement(sessionUser.getSubscribeVideos(), id);
                updateOne(query, "subscribe_videos", sessionUser.getSubscribeVideos());
                break;
            case 3:
                setElement(sessionUser.getSubscribeArticles(), id);
                updateOne(query, "subscribe_articles", sessionUser.getSubscribeArticles());
                break;
            case 4:
                setElement(sessionUser.getChatTargetIds(), id);
                updateOne(query, "chat_target_ids", sessionUser.getChatTargetIds());
                break;
        }

    }

    /**
     * 将set集合中元素element的状态取反
     * @param set 集合
     * @param element 元素
     */
    private void setElement(Set<String> set, String element) {
        if(set.contains(element)) {
            set.remove(element);
        } else {
            set.add(element);
        }
    }
}
